//
//  CCRPhoneViewController.m
//  Activity
//
//  Created by its on 15/5/7.
//  Copyright (c) 2015年 coco. All rights reserved.
//

#import "CCRPhoneViewController.h"
#import <QuartzCore/QuartzCore.h>
#import <ReactiveCocoa/ReactiveCocoa.h>
#import <AVOSCloud/AVOSCloud.h>
#import "CCRPhoneViewModel.h"
#import "CCRouter.h"
@interface CCRPhoneViewController ()
@property (nonatomic, strong) UITextField *phone;
@property (nonatomic, strong) UIButton *agree;
@property (nonatomic, strong) UILabel *text;
@property (nonatomic, strong) UIButton *deal;
@property (nonatomic, strong) UIButton *next;
@property (nonatomic, strong) CCRPhoneViewModel *viewModel;
@end

@implementation CCRPhoneViewController

- (id) initWithViewModel:(CCRPhoneViewModel *) viewModel {
  if (self = [super init]) {
    _viewModel = viewModel;
  }
  return self;
}

- (void)viewDidLoad {
  self.title = @"注册";
  [self setupUI];
  [self setupCSS];
  [self bindUI];
  [super viewDidLoad];
  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void) setupUI {
  self.view.backgroundColor = RGB(236, 237, 236, 1);
  _phone = [[UITextField alloc] init];
  _phone.backgroundColor = [UIColor whiteColor];
  _phone.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 4, 1)];
  _phone.leftViewMode = UITextFieldViewModeAlways;
  _phone.layer.cornerRadius = 5.0f;
  _phone.layer.borderWidth = 0.1f;
  _phone.placeholder = @"请输入手机号";
  _agree = [UIButton buttonWithType:UIButtonTypeCustom];
  [_agree setImage:IMAGE(@"no_select") forState:UIControlStateNormal];
  
  _text = [[UILabel alloc] init];
  _text.text = @"同意";
  _deal = [UIButton buttonWithType:UIButtonTypeCustom];
  _next = [UIButton buttonWithType:UIButtonTypeCustom];
  [_deal setTitle:@"《用户注册协议》" forState:UIControlStateNormal];
  [_deal setTitleColor:RGB(51, 145, 94, 1) forState:UIControlStateNormal];
  [_next setTitle:@"下一步" forState:UIControlStateNormal];
  [_next setTitleColor:RGB(255, 255, 255, 1) forState:UIControlStateNormal];
  [_next setBackgroundImage:IMAGE(@"btn_normal") forState:UIControlStateNormal];
  
  [self.view addSubview:_phone];
  [self.view addSubview:_agree];
  [self.view addSubview:_text];
  [self.view addSubview:_deal];
  [self.view addSubview:_next];
}

- (void) setupCSS {
  [_phone mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.view).offset(20);
    make.top.equalTo(self.view).offset(20 );
    make.right.equalTo(self.view).offset(-20);
    make.height.mas_equalTo(40);
  }];
  [_agree mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.view).offset(20);
    make.top.equalTo(self.phone.mas_bottom).offset(15);
    make.size.mas_equalTo(CGSizeMake(21, 21));
  }];
  [_text mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.agree.mas_right).offset(5);
    make.top.equalTo(self.phone.mas_bottom).offset(15);
  }];
  
  [_deal mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.text.mas_right).offset(0);
    make.top.equalTo(self.phone.mas_bottom).offset(15);
    make.size.mas_equalTo(CGSizeMake(145, 23));
  }];
  
  [_next mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(self.view).offset(20);
    make.top.equalTo(_deal.mas_bottom).offset(20);
    make.right.equalTo(self.view).offset(-20);
    make.height.mas_equalTo(40);
  }];
}

- (void) bindUI {
    @weakify(self);
  self.agree.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
    @strongify(self);
    (self.viewModel.isAgree.intValue == 1)?(self.viewModel.isAgree = @(0)):(self.viewModel.isAgree = @(1));
    if (self.viewModel.isAgree.intValue == 1) {
      [self.agree setImage:IMAGE(@"selected") forState:UIControlStateNormal];
    } else {
      [self.agree setImage:IMAGE(@"no_select") forState:UIControlStateNormal];
    }
    return [RACSignal empty];
  }];
  self.next.rac_command = self.viewModel.nextCommand;
  RAC(self.viewModel , text) = self.phone.rac_textSignal;

  [self.viewModel.nextSignal subscribeNext:^(CCError *x) {
    @strongify(self);
    if (x.code == 0) {
      [CCProgressHUD hiddenHUBFromView:self.view];
      [CCRouter routerWithURL:@"rsms" param:@{@"phone":self.viewModel.text}];
    } else if (x.code == 1){
      [CCProgressHUD showHUBInView:self.view withTimeOut:0 str:x.des];
    } else {
      [CCProgressHUD showMessage:self.view withTimeOut:3.0f str:x.des];
    }
  } error:^(NSError *error) {

  } completed:^{
    
  }];
}
@end
